package com.bbs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bbs.entity.User;
import com.bbs.mapper.UserMapper;
import com.bbs.service.SecurityLoginService;
import com.bbs.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
@RequiredArgsConstructor
public class SecurityLoginServiceImpl implements SecurityLoginService {
    private final UserService userService;
    private final UserMapper userMapper;
    @Override
    public UserDetails loadUserByUsername(String phone) throws UsernameNotFoundException {
      //获取用户名
        //根据用户名去查询数据
        LambdaQueryWrapper<User> lam=new LambdaQueryWrapper<>();
        lam.eq(User::getPhone,phone);
        User one = userService.getOne(lam);
        return org.springframework.security.core.userdetails.User.builder()
                .username(one.getPhone())
                .password(one.getPassword())
                .authorities(AuthorityUtils.NO_AUTHORITIES)
                .build();
        //返回userDetails给security给框架后，框架会采用密码加密进行比对
    }
}
